#include "DatabaseWork.h"

using namespace DatabaseModule;

bool DatabaseWork::createConnection() {
	if (db.isOpen())
		db.close();

	db = QSqlDatabase::addDatabase("QSQLITE");
	db.setDatabaseName(dbName);

	if (!db.open()) {
		qDebug() << "Error in " << __FUNCTION__ << ". " << db.lastError();

		return false;
	}

	return true;
}

bool DatabaseWork::execSqlQuery(const QString& query) {
	if (!db.isOpen() && !createConnection()) {
		qDebug() << "Error in " << __FUNCTION__ << ". " << "Cannot open DB";

		return false;
	}

	QSqlQuery sqlQuery;

	if (!sqlQuery.exec(query)) {
		qDebug() << "Error in " << __FUNCTION__ << ". " << sqlQuery.lastError();
	}

	return true;
}

bool DatabaseWork::execSqlQuery(const QString& query, QSqlQuery& result) {
	if (!db.isOpen() && !createConnection()) {
		qDebug() << "Error in " << __FUNCTION__ << ". " << "Cannot open DB";

		return false;
	}

	QSqlQuery sqlQuery;

	if (!sqlQuery.exec(query)) {
		qDebug() << "Error in " << __FUNCTION__ << ". " << sqlQuery.lastError();
	}

	result = sqlQuery;

	return true;
}

QueryModelPtr DatabaseWork::getSqlModel(const QString& query) {
	QueryModelPtr queryResult = NULL;

	if (!db.isOpen() && !createConnection()) {
		qDebug() << "Error in " << __FUNCTION__ << ". " << "Cannot open DB";
		//queryResult->clear();

		return queryResult;
	}	

	queryResult = new QSqlQueryModel();

	queryResult->setQuery(query);

	if (queryResult->lastError().isValid()) {
		qDebug() << "Error in " << __FUNCTION__ << ". " << queryResult->lastError();

		queryResult->clear();
	}

	return queryResult;
}

TableModelPtr DatabaseWork::getTableModel(const QString& tableName) {
	TableModelPtr model = NULL;

	if (!db.isOpen() && !createConnection()) {
		qDebug() << "Error in " << __FUNCTION__ << ". " << "Cannot open DB";
		//model->clear();

		return model;
	}

	model = new QSqlTableModel();

	model->setTable(tableName);

	if (model->lastError().isValid()) {
		qDebug() << "Error in " << __FUNCTION__ << ". " << model->lastError();

		model->clear();
	}

	model->select();
	model->setEditStrategy(QSqlTableModel::OnRowChange);
	
	return model;
}

void DatabaseWork::initTransaction() {
	if (!db.isOpen() && !createConnection()) {
		qDebug() << "Error in " << __FUNCTION__ << ". " << "Cannot open DB";

		return;
	}

	QSqlQuery *q = new QSqlQuery();

	transactionPtr.reset(q);
	inTransaction = true;
}

void DatabaseWork::setTransactionTemplate(const QString& templ) {
	if (!db.isOpen() && !createConnection()) {
		qDebug() << "Error in " << __FUNCTION__ << ". " << "Cannot open DB";

		return;
	}

	if (!inTransaction)
		initTransaction();

	transactionPtr->prepare(templ);
}

bool DatabaseWork::addTransactionValues(const QVariantList& values) {
	if (!inTransaction) {
		qDebug() << "Error in " << __FUNCTION__ << ". " << "Transaction is not created";

		return false;
	}

	transactionPtr->addBindValue("adb_anime");
	transactionPtr->addBindValue("Adb_anme");
	transactionPtr->addBindValue("sdsds");
	transactionPtr->addBindValue("");
	qDebug() << transactionPtr->boundValues();

	//transactionPtr->addBindValue(values);

	return true;
}

bool DatabaseWork::execTransaction() {
	if (!inTransaction) {
		qDebug() << "Error in " << __FUNCTION__ << ". " << "Transaction is not created";

		return false;
	}

	qDebug() << transactionPtr->boundValues();

	if (!transactionPtr->execBatch()) {
		qDebug() << "Error in " << __FUNCTION__ << ". " << transactionPtr->lastError();

		clearTransaction();

		return false;
	}
	
	qDebug() << transactionPtr->lastQuery();
	clearTransaction();

	return true;
}

void DatabaseWork::clearTransaction() {
	if (inTransaction) {
		transactionPtr->clear();
		inTransaction = false;
	}
}